package com.hnkywl.sts.action.organ;

import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.organ.Nbbm;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.organ.NbbmService;
import com.hnkywl.sts.service.organ.NbgsService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 说明：
 * 包:        com.hnkywl.sts.action.sys
 * 类描述:    岗位Action类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Namespace("/organ/gsjg")
public class GsjgAction extends SimpleActionSupport {
    private GsjgService gsjgService;
    private NbbmService nbbmService;
    private NbgsService nbgsService;
    //公司机构
    private Gsjg gsjg;
    //公司机构名称
    private String mc;
    //传入的id值
    private Long gsjgId;
    //操作类型 add表示新增,update表示修改
    private String operation;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //父机构Id
    private Long parentId;
    //父机构名称
    private String parentMc;
    //远程ajax验证的值
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;
    //内部部门和内部公司的区别
    private Integer type;
    //内部部门
    private Nbbm nbbm;
    //内部公司
    private Nbgs nbgs;
    //经营机构根ID
    private Long rootId;
    //验证的类型
    private Long sjjgId;

    private Long jgId;

    private String jc;

    private String qc;

    private Long htId;

    //公司机构的查询分页
    @Action("gsjgList")
    public void gsjgList() {
        if (type != null && type == 0) {
            this.renderText(this.nbbmService.findAllNbbm(gsjgId, mc, parentId, parentMc, page, rows));
        } else if (type != null && type == 1) {
            this.renderText(this.nbgsService.findAllNbgs(gsjgId, mc, parentId, parentMc, page, rows));
        } else {
            this.renderJson(this.gsjgService.findAllGsjg(gsjgId, mc, parentId, parentMc, page, rows));
        }
    }

    @Action("gsjgMcList")
    public void gsjgMcList() {
        if (type != null && type == 0) {
            this.renderJson(this.nbbmService.findAllNbbmMc(gsjgId, null, null, null, 0, -1));
        } else if (type != null && type == 1) {
            this.renderJson(this.nbgsService.findAllNbgsMc(gsjgId, null, null, null, 0, -1));
        } else {
            this.renderJson(this.gsjgService.findAllGsjgMc(gsjgId, null, null, null, 0, -1));
        }
    }


    @Action("nbgsList")
    public void nbgsList() {
        this.renderText(this.nbgsService.findAllNbgs());
    }

    //公司机构树
    // type=0 内部部门
    // type=1 内部公司
    // type=null 查询所有根节点公司机构
    @Action("gsjg_tree")
    public void gsjgTree() {
        try {
            if (type != null && type == 0) {
                this.renderJson(this.nbbmService.findAllNbbm(gsjgId));
            } else if (type != null && type == 1) {
                this.renderJson(this.nbgsService.findNbgsTree(gsjgId));
            } else {
                this.renderJson(this.gsjgService.findGsjgTree(gsjgId));
            }
        } catch (Exception e) {
            this.renderJson("");
        }
    }

    //公司机构树-经营机构
    @Action("findGsOrJg")
    public void findGsOrJg() {
        Gsjg g = this.gsjgService.findGsjgDetail(jgId);
        if (g instanceof Nbgs) {
            this.renderText("1");
        } else {
            this.renderText("2");
        }
    }

    //公司机构树-经营机构
    @Action("gsjg_tree_jy")
    public void gsjgTreJy() {
        try {
            this.renderJson(this.gsjgService.findGsjgTreeJy(null, true));
        } catch (Exception e) {
            this.renderJson("");
        }
    }

    //公司机构树-无权限
    @Action("gsjg_tree_wqx")
    public void gsjgwqxTreJy() {
        try {
            this.renderJson(this.gsjgService.findGsjgwqxTreeJy(null, true));
        } catch (Exception e) {
            this.renderJson("");
        }
    }
    //所属公司机构树
    @Action("gsjg_sstree")
    public void SsgsjgTree() {
        try {
            this.renderJson(this.gsjgService.findSsGsjgTree(null, true));
        } catch (Exception e) {
            this.renderJson("");
        }
    }

    //公司本部下拉框
    @Action("gsbb")
    public void getGsbb() {
        try {
            this.renderJson(this.gsjgService.getGsbb());
        } catch (Exception e) {
            this.renderJson("");
        }
    }

    //公司机构树-经营机构
    @Action("gsjg_tree_jychild")
    public void gsjgTreeJyChild() {
        try {
            this.renderJson(this.gsjgService.findGsjgTreeJy(rootId, false));
        } catch (Exception e) {
            this.renderJson("");
        }
    }

    //获得自己够的名称列表，用于级联
    @Action("childgsjg_json")
    public void getChildren() {
        renderJson(this.gsjgService.findChildren(gsjgId, type));
    }

    //获得自己够的名称列表，用于级联 (级联部门为经营部门)
    @Action("childjygsjg_json")
    public void getJyChildren() {
        renderJson(this.gsjgService.findJyChildren(gsjgId, type));
    }


    @Action("childbmwithoutqx_json")
    public void getChildrenBmNotQx() {
        renderJson(this.gsjgService.findChildrenBmNotWithQx(gsjgId, type));
    }

    @Action("allgsjg_tree")
    public void allGsjgTree() {
        try {
            this.renderJson(this.gsjgService.findAllGsjgTree(gsjgId));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据公司Id获取供应站列表
     *
     * @return
     */
    @Action("gyz_combobox")
    public void gyzComboboxBygsId() {
        this.renderJson(this.nbbmService.getGyzJson(gsjgId));
    }

    /**
     * 获取经营部门
     *
     * @return
     */
    @Action("jybm")
    public void findJybm() {
        this.renderJson(nbbmService.findJybm(gsjgId));
    }

    /**
     * 八大保供业务部门
     *
     * @return
     */
    @Action("bgywbm")
    public void findBgywbm() {
        this.renderJson(nbbmService.findBgywbm());
    }

    @Action("gsjg_content")
    public String gsjgContent() {
        //进入update页面
        if ("update".equals(operation)) {
            gsjg = gsjgService.findGsjgDetail(gsjgId);
            if (gsjg instanceof Nbbm) {
                nbbm = (Nbbm) gsjg;
                type = 0;
            } else {
                nbgs = (Nbgs) gsjg;
                type = 1;
            }
        } else {
            if (gsjgId != null)
                gsjg = gsjgService.findGsjgDetail(gsjgId);
            nbbm = new Nbbm();
            //nbbm.setBm(gsjgService.findBm());
            if (gsjg != null && !gsjg.getSfzx())
                nbbm.setSjjg(gsjg);
            if (gsjgId != null)
                nbbm.setPxh(gsjgService.findNextPxh(gsjgId));
            nbgs = new Nbgs();
            //nbgs.setBm(gsjgService.findBm());
            if (gsjg != null && !gsjg.getSfzx() && gsjg instanceof Nbgs)
                nbgs.setSjjg(gsjg);
            if (gsjgId != null)
                nbgs.setPxh(gsjgService.findNextPxh(gsjgId));
        }
        return SUCCESS;
    }

    @Action("gsbmfwsz")
    public String bmfwsz() {
        return SUCCESS;
    }

    //根据当前的上级部门获得下一个排序号
    @Action("get_nextpxh")
    public void getNextPxh() {
        renderText(gsjgService.findNextPxh(gsjgId).toString());
    }

    @Action("save_gsjg")
    public void save() {
        try {
            if (type != null && type == 0) {
                if (nbbm.getId() == null)
                    nbbmService.saveNbbm(nbbm);
                else
                    nbbmService.updateNbbm(nbbm);
            } else {
                if (nbgs.getId() == null)
                    nbgsService.saveNbgs(nbgs);
                else
                    nbgsService.updateNbgs(nbgs);
            }
            renderText("{\"statusCode\":\"200\", \"message\":\"操作成功\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     start_gsjg
     * 功能描述:      启用公司机构
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("start_gsjg")
    public void startGsjg() {
        try {
            this.gsjgService.startGsjg(gsjgId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"启用成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 远程ajax验证
     */
    @Action("ajax_validate")
    public void validate1() {
        if (!gsjgService.ajaxValidate(gsjgId, validateVal, validateField, sjjgId))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    /**
     * 方法名:     stop_gsjg
     * 功能描述:      停用公司机构
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("stop_gsjg")
    public void stopGsjg() {
        try {
            this.gsjgService.stopGsjg(gsjgId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"停用成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     delete_gsjg
     * 功能描述:      删除公司机构
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-11上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-11上午11:50
     */
    @Action("delete_gsjg")
    public void deleteGsjg() {
        try {
            this.gsjgService.deleteGsjg(gsjgId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"删除机构成功\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    /**
     * 是否选中正确的企业法人提供的远程验证
     */
    @Action("ajax_contrary_validate")
    public void contraryValidate() {
        if (gsjgService.ajaxValidateGsjg(gsjgId, validateVal, validateField, type))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    @Action("findKmdm")
    public void findKmdm() {
        renderJson(nbbmService.findKmdm(gsjgId));
    }

    //查询所有的企业机构简称
    @Action("gsjgandqyjg_list")
    public void gsjgAndqyjgJcList() {
        this.renderJson(this.gsjgService.findGsjgAndQyjg(jc, 1, -1));
    }

    @Action("gsjgandqyjg_qclist")
    public void gsjgAndqyjgQcList() {
        this.renderJson(this.gsjgService.findGsjgAndQyjgByQc(qc, 1, -1));
    }

    @Action("findXyKhByHtId_qclist")
    public void findXyKhByHtId_qclist() {
        this.renderJson(this.gsjgService.findXyKhByHtId_qclist(htId,qc, 1, -1));
    }

    @Action("findNbgsByHtId_qclist")
    public void findNbgsByHtId_qclist() {
        this.renderJson(this.gsjgService.findNbgsByHtId_qclist(htId,qc, 1, -1));
    }


    //=========================getterandSetter================================


    public String getJc() {
        return jc;
    }

    public void setJc(String jc) {
        this.jc = jc;
    }

    public Gsjg getGsjg() {
        return gsjg;
    }

    public void setGsjg(Gsjg gsjg) {
        this.gsjg = gsjg;
    }

    public String getMc() {
        return mc;
    }

    public void setMc(String mc) {
        this.mc = mc;
    }

    public Long getGsjgId() {
        return gsjgId;
    }

    public void setGsjgId(Long gsjgId) {
        this.gsjgId = gsjgId;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getParentMc() {
        return parentMc;
    }

    public void setParentMc(String parentMc) {
        this.parentMc = parentMc;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Nbbm getNbbm() {
        return nbbm;
    }

    public Long getSjjgId() {
        return sjjgId;
    }

    public void setSjjgId(Long sjjgId) {
        this.sjjgId = sjjgId;
    }

    public void setNbbm(Nbbm nbbm) {
        this.nbbm = nbbm;
    }

    public Nbgs getNbgs() {
        return nbgs;
    }

    public void setNbgs(Nbgs nbgs) {
        this.nbgs = nbgs;
    }

    public String getQc() {
        return qc;
    }

    public void setQc(String qc) {
        this.qc = qc;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public String getValidateField() {
        return validateField;
    }

    public Long getJgId() {
        return jgId;
    }

    public void setJgId(Long jgId) {
        this.jgId = jgId;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }

    @Autowired
    public void setNbbmService(NbbmService nbbmService) {
        this.nbbmService = nbbmService;
    }

    @Autowired
    public void setNbgsService(NbgsService nbgsService) {
        this.nbgsService = nbgsService;
    }

    public void setRootId(Long rootId) {
        this.rootId = rootId;
    }

    public Long getHtId() {
        return htId;
    }

    public void setHtId(Long htId) {
        this.htId = htId;
    }
}
